package cn.xshi.oauth.service.impl;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.xshi.oauth.dao.OauthAccountRoleDao;
import cn.xshi.oauth.model.OauthAccount;
import cn.xshi.oauth.model.OauthAccountRole;
import cn.xshi.oauth.service.OauthAccountRoleService;
import cn.xshi.oauth.util.OauthUtil;
import cn.xshi.common.base.BaseService;
import cn.xshi.common.entity.RoleInfoEntity;
import cn.xshi.common.util.ExceptionUtil;
import cn.xshi.common.util.StringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
/**
 * @Desc 授权中心账户对角色
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@Service
public class OauthAccountRoleServiceImpl extends BaseService implements OauthAccountRoleService {

	@Resource
	private OauthAccountRoleDao oauthAccountRoleDao;

	@Resource
    OauthUtil oauthUtil;

	/**
	* 分页
	* @param condition 
	* @return
	*/
	public List<OauthAccountRole> getOauthAccountRoleListByCondition(Map<String,Object> condition){
		try{
			return oauthAccountRoleDao.getOauthAccountRoleListByCondition(condition);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}

	/**
	* 添加
	* @param oauthAccountRoleList
	* @return
	*/
	public int addOauthAccountRole(RoleInfoEntity roleinfoEntity, List<OauthAccountRole> oauthAccountRoleList){
		int i = 0;
		try {
			for(int j = 0; j < oauthAccountRoleList.size(); j++){
				oauthAccountRoleDao.addOauthAccountRole(oauthAccountRoleList.get(j));
			}
			//处理变更功能资源
			if(!StringUtil.isEmpty(roleinfoEntity.getAccountId())){
				String[] accountIdArray = roleinfoEntity.getAccountId().split(",");
				List<String> accountIdList = new ArrayList<>();
				if(null != accountIdArray){
					for(String id: accountIdArray){
						accountIdList.add(id);
					}
				}
				oauthUtil.doTokenResources(roleinfoEntity.getRoleId(),accountIdList);
			}
			i = 1;
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	* 删除
	* @param condition 
	* @return
	*/
	public int delOauthAccountRole(Map<String,Object> condition){
		int i = 0;
		try {
			i = oauthAccountRoleDao.delOauthAccountRole(condition);
			//处理变更功能资源
			if(null != condition.get("accountId")){
				String[] accountIdArray = (String[])condition.get("accountId");
				List<String> accountIdList = new ArrayList<>();
				if(null != accountIdArray){
					for(String id: accountIdArray){
						accountIdList.add(id);
					}
				}
				oauthUtil.doTokenResources(String.valueOf(condition.get("roleId")),accountIdList);
			}

		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	 *
	 * @param condition
	 * @return
	 */
	public List<OauthAccount> getOauthARListByCondition(Map<String, Object> condition){
		try{
			return oauthAccountRoleDao.getOauthARListByCondition(condition);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}

	/**
	 * 批量删除
	 * @param condition
	 * @return
	 */
	/**
	 * 批量删除
	 * @param condition
	 * @return
	 */
	public int delBatchOauthAccountRole(Map<String, Object> condition){
		int i = 0;
		try {
			i = oauthAccountRoleDao.delBatchOauthAccountRole(condition);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}
}
